package org.display.graphic
{
	import flash.display.Sprite;
	import flash.events.*;
	import flash.geom.Point;
	import flash.ui.*;
	import flash.utils.*;
	
	import mx.controls.*;
	import mx.flash.UIMovieClip;
	import mx.managers.*;
	
	import org.abcFrame.core.*;
	import org.data.*;
	import org.display.graphic.*;
	import org.display.util.*;
	import org.events.*;
	import org.utils.ElementControl;
	import org.enmu.*
	
	//import spark.components.WindowedApplication;
	
	public class SceneBg extends UIMovieClip
	{

		protected var areaContainer:Sprite
		
		protected var app:Object
		
		protected var shapeArea:ShapeArea
		
		protected var point:Point
		
		public function SceneBg(_areaContainer:Sprite,app:Object)
		{
			this.app=app
			
			this.areaContainer=_areaContainer

			this.addEventListener(MouseEvent.MOUSE_DOWN,mouseEvent)
			
			this.addEventListener(MouseEvent.RIGHT_MOUSE_DOWN,mouseEvent)

			draw(this,Global.sceneSize,Global.sceneSize)
			
			shapeArea=new ShapeArea(areaContainer)
				
			createMenuItems(["添加<bt>","添加<mc>","添加<tf>","添加<mbt>","清空"])
			
		}
		
		function draw(obj:Sprite,w:Number,h:Number){
			
			with(obj){
				graphics.clear()
				graphics.lineStyle(1,0x000000,1)
				graphics.beginFill(0xcccccc,0.5)
				graphics.drawRect(0,0,w,h)
				graphics.endFill()
			}
			
		}
		

		protected function mouseEvent(e:MouseEvent){
			
			switch(e.type){
				
				case MouseEvent.MOUSE_DOWN:
					
				shapeArea.mousedownEvent()
					
				areaContainer.addChild(shapeArea)
				point=new Point(areaContainer.mouseX,areaContainer.mouseY)

				shapeArea.x=point.x
				shapeArea.y=point.y
				
				stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseEvent)
				stage.addEventListener(MouseEvent.MOUSE_UP,mouseEvent)
				
				break
				
				case MouseEvent.MOUSE_MOVE:

				shapeArea.redraw(areaContainer.mouseX-point.x,areaContainer.mouseY-point.y)
				
				break
				
				case MouseEvent.MOUSE_UP:
					
				shapeArea.mouseupEvent()

				stage.removeEventListener(MouseEvent.MOUSE_MOVE,mouseEvent)
				stage.removeEventListener(MouseEvent.MOUSE_UP,mouseEvent)
				break
				
				case MouseEvent.RIGHT_MOUSE_DOWN:
					
				shapeArea.mousedownEvent()
				
				/*
				var myContextMenu:ContextMenu = new ContextMenu();
				myContextMenu.hideBuiltInItems();
				
				createMenuItems(["添加<bt>","添加<mc>","添加<tf>","添加<mbt>","添加<清空>"])
				
				
				createItem("添加<bt>")
				createItem("添加<mc>")
				createItem("添加<tf>")
				createItem("添加<mbt>")
				//createItem("发布成对象类")
				
				//createItem("保存")
				//createItem("发布成文档类")
				
				createItem("清空")
				
				this.contextMenu = myContextMenu;*/
				
				break
			}
			
			
			
			
			
		}
		
		public function createMenuItems(items:Array){
			
			var myContextMenu:ContextMenu = new ContextMenu();
			myContextMenu.hideBuiltInItems();
			
			for(var i=0;i<items.length;i++){
				
				createItem(items[i])
				
			}
			
			this.contextMenu = myContextMenu;
			
			function createItem(vlaue:String){
				
				var item:ContextMenuItem = new ContextMenuItem(vlaue);
				item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT ,menuselectEvent)
				myContextMenu.customItems.push(item);
				
			}
			
		}
		
		protected function menuselectEvent(e:ContextMenuEvent){
			
			var shape:ShapeBase
			
			switch(e.currentTarget.caption){
				
				case "保存":
					
					try{

					var reg:RegExp=/\/org\/.*?(?=\/view\/)|\\org\\.*?(?=\\view\\)/gis
					Global.lockPath=reg.exec(Global.xmlPath).toString().replace(/\//gis,"\\")
				    
				    }catch(e:Error){trace(e)}
					
					//修改窗口提示
					app.title="AcbFrame—"+"锁定发布文件夹"+Global.lockPath+"—"+Global.workspace
					
					if(Global.xmlPath!="")
					{	
						var addFolderPannel:ETitlePannel=ETitlePannel(PopUpManager.createPopUp( this, ETitlePannel , true));
						
						if(Global.xmlPath.indexOf("\\module\\")!=-1||Global.xmlPath.indexOf("\/module\/")!=-1)
						{	
							addFolderPannel.title="发布成文档类"
							
						}else{
							
							addFolderPannel.title="发布成对象类"
						}
						
						try{
							
						reg=/(?<=\\view\\).*?(?=View.xml)|(?<=\/view\/).*?(?=View.xml)/gis	
						addFolderPannel.userName.text=reg.exec(Global.xmlPath).toString()
							
						}catch(e:Error){trace(e)}
							
					}
					
					break
				
				case "添加<bt>":
					shape=new Bt()
					shape.x=areaContainer.mouseX
					shape.y=areaContainer.mouseY
					areaContainer.addChild(shape)
					break
				
				case "添加<mc>":
					shape=new Mc()
					shape.x=areaContainer.mouseX
					shape.y=areaContainer.mouseY
					areaContainer.addChild(shape)
					
					break
				
				case "添加<tf>":
					shape=new Tf()
					shape.x=areaContainer.mouseX
					shape.y=areaContainer.mouseY
					areaContainer.addChild(shape)
					
					break
				
				case "添加<mbt>":
					
					shape=new Mbt()
					shape.x=areaContainer.mouseX
					shape.y=areaContainer.mouseY
					areaContainer.addChild(shape)

					break
				
				case "发布成对象类":
					
					if(Global.lockPath==""){
						Alert.show("请锁定[org/display]或是此路径下任一文件夹作为发布文件夹!")
						return;
					}
					
					var addFolderPannel:ETitlePannel=ETitlePannel(PopUpManager.createPopUp( this, ETitlePannel , true));
					addFolderPannel.title=e.currentTarget.caption
					
					break
				
				case "发布成文档类":
					
					if(Global.lockPath==""){
						Alert.show("请锁定[org/display]或是此路径下任一文件夹作为发布文件夹!")
						return;
					}
					
					var addFolderPannel:ETitlePannel=ETitlePannel(PopUpManager.createPopUp( this, ETitlePannel , true));
					addFolderPannel.title=e.currentTarget.caption
					
					break
				
				case "清空":
					ElementControl.cleanObjByType(ShapeBase,areaContainer)
					break
				
				
			}
			
		}
		
		
	}
}